
#include "PacketEnumerations.h"
#include "RakNetworkFactory.h"
#include "RakClientInterface.h"
#include "RakServerInterface.h"
#include "NetworkTypes.h"
#include "BitStream.h"

#include "Ship.h"

#include <string>
#include <windows.h>
#include <cstdio>
#include <conio.h>

int main()
{

    RakServerInterface *rakServer = 0;
    RakClientInterface *rakClient = 0;

    bool bIsServer;

    printf("Hit 'h' to host\n");
    if (getch() == 'h') {

        bIsServer = true;

        rakServer = RakNetworkFactory::GetRakServerInterface();
        rakServer->Start(8, 0, 30, 60000);

        printf("Server started...\n");

        char message[256];

        while(1) {

            if (kbhit())
            {

                gets(message);

                if (strcmp(message, "quit")==0)
                {
                    puts("Quitting.");
                    break;
                }
                else
                {

                    rakServer->Send(message, (const int) strlen(message)+1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);

                }

            }

            Packet* packet = rakServer->Receive();
            if (packet) {

                // We got a packet, get the identifier with our handy function
             unsigned char packetIdentifier = packet->data[0];

                // Check if this is a network message packet
                switch (packetIdentifier)
                {
                   case ID_NEW_INCOMING_CONNECTION:
                         // Somebody connected.  We have their IP now
                        printf("ID_NEW_INCOMING_CONNECTION\n");
                        break;
                    default:

                        char sendData[256];

                        sprintf(sendData, "%s", packet->data);

                        //Relay the message
                        rakServer->Send(sendData, (const int) strlen(sendData)+1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->playerId, true);
                        
                        // It's a client, so just show the message
                        printf("%s - %s\n", packet->playerId.ToString(), packet->data);
                        break;
                }

            }

        }
        /*char str[100];

        printf("Enter string to send: \n");
        gets(str+1);
        str[0] = ID_RESERVED9+1;

        rakPeer->Send((char*)&str, strlen(str)+1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);
        */

    }
    else {

        bIsServer = false;

        rakClient = RakNetworkFactory::GetRakClientInterface();
        rakClient->Connect("127.0.0.1", 60000, 0, 0, 30);

        printf("Connecting...\n");

        while(!rakClient->IsConnected()) {

            Sleep(100);

        }

        printf("Connected\n");

        char message[256];

        while(1) {

            if (kbhit())
            {

                gets(message);

                if (strcmp(message, "quit")==0)
                {
                    puts("Quitting.");
                    break;
                }
                else
                {

                    rakClient->Send(message, (int) strlen(message)+1, HIGH_PRIORITY, RELIABLE_ORDERED, 0);

                }

            }

            Packet* packet = rakClient->Receive();
            if (packet) {

                // We got a packet, get the identifier with our handy function
                unsigned char packetIdentifier = packet->data[0];

                // Check if this is a network message packet
                switch (packetIdentifier)
                {
                    case ID_CONNECTION_REQUEST_ACCEPTED:
                        // This tells the client they have connected
                        printf("ID_CONNECTION_REQUEST_ACCEPTED\n");
                        break;
                    default:
                        // It's a client, so just show the message
                        printf("%s - %s\n", packet->playerId.ToString(), packet->data);
                        break;
                }

            }

        }

    }

    if (bIsServer) {
        rakServer->Disconnect(300);
        // We're done with the network
        RakNetworkFactory::DestroyRakServerInterface(rakServer);
    }
    else {
        rakClient->Disconnect(300);
        // We're done with the network
        RakNetworkFactory::DestroyRakClientInterface(rakClient);
    }

    printf("Finished...\n");

    /*while(1) {


        Packet* packet = rakPeer->Receive();
        if (packet) {

            if (packet->data[0] == ID_RESERVED9+1) {

                printf("Str= %s\n", packet->data+1);

            }

        }

        Sleep(100);

    }

    RakNetworkFactory::DestroyRakPeerInterface(rakPeer);
    */

}



















/*



int main()
{

    RakPeerInterface *rakPeer = RakNetworkFactory::GetRakPeerInterface();

    printf("Hit 'h' to host\n");
    if (getch() == 'h') {

        rakPeer->Initialize(1, 60001, 0, 0);
        rakPeer->Connect("127.0.0.1", 60000, 0, 0);
        printf("Connecting...\n");
        while(rakPeer->GetPlayerIDFromIndex(0) == UNASSIGNED_PLAYER_ID) {

            Sleep(30);

        }
        printf("Connected\n");

        char str[100];

        printf("Enter string to send: \n");
        gets(str+1);
        str[0] = ID_RESERVED9+1;

        rakPeer->Send((char*)&str, strlen(str)+1, HIGH_PRIORITY, RELIABLE_ORDERED, 0, UNASSIGNED_PLAYER_ID, true);

    }
    else {

        rakPeer->Initialize(1, 60000, 0, 0);
        rakPeer->SetMaximumIncomingConnections(1);

    }

    printf("Looping...\n");

    while(1) {


        Packet* packet = rakPeer->Receive();
        if (packet) {

            if (packet->data[0] == ID_RESERVED9+1) {

                printf("Str= %s\n", packet->data+1);

            }

        }

        Sleep(100);

    }

    RakNetworkFactory::DestroyRakPeerInterface(rakPeer);

}




























#include "Example\ExampleApplication.h"

// Declare a subclass of the ExampleFrameListener class
class MyListener : public ExampleFrameListener
{
public:
    MyListener(RenderWindow* win, Camera* cam) : ExampleFrameListener(win, cam)
    {
    }

    bool frameStarted(const FrameEvent& evt)
    {
        return ExampleFrameListener::frameStarted(evt);
    }

    bool frameEnded(const FrameEvent& evt)
    {
        return ExampleFrameListener::frameEnded(evt);
    }
};

// Declare a subclass of the ExampleApplication class
class SampleApp : public ExampleApplication
{
public:
   SampleApp()
   {
   }

protected:
   // Define what is in the scene
   void createScene(void)
   {
       // put your scene creation in here
   }

   // Create new frame listener
   void createFrameListener(void)
   {
       mFrameListener = new MyListener(mWindow, mCamera);
       mRoot->addFrameListener(mFrameListener);
   }
};

#ifdef __cplusplus
extern "C" {
#endif

#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
#define WIN32_LEAN_AND_MEAN
#include "windows.h"
INT WINAPI WinMain(HINSTANCE hInst, HINSTANCE, LPSTR strCmdLine, INT)
#else
int main(int argc, char **argv)
#endif
{
    // Instantiate our subclass
    SampleApp myApp;

    try {
        // ExampleApplication provides a go method, which starts the rendering.
        myApp.go();
    }
    catch (Ogre::Exception& e) {
#if OGRE_PLATFORM == OGRE_PLATFORM_WIN32
        MessageBoxA(NULL, e.getFullDescription().c_str(), "An exception has occured!", MB_OK | MB_ICONERROR | MB_TASKMODAL);
#else
        std::cerr << "Exception:\n";
        std::cerr << e.getFullDescription().c_str() << "\n";
#endif
        return 1;
    }

return 0;
}

#ifdef __cplusplus
}
#endif

*/
